<%--
  Created by IntelliJ IDEA.
  User: zuxia
  Date: 2018/8/20
  Time: 16:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>需求列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../frame/layui/css/modules/layer/default/layer.css">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title">
    <legend>查看需求</legend>
</fieldset>

<div class="tail">
    <div class="head">

        <div class="my-btn-box" style="margin-left: 50px;">
            <a href="../application/addApplication.jsp" style="color: white;">
                <button class="layui-btn layui-btn-sm">发布需求</button>
            </a>
            <div style="float: right; margin-right: 50px;">
    <span class="fr">
        <span class="layui-form-label">搜索条件：</span>
        <div class="layui-input-inline">
            <input type="text" id="sousuo" autocomplete="off" placeholder="请输入搜索条件" class="layui-input">
        </div>
        <button class="layui-btn mgl-20" onclick="queryByKey()">查询</button>
    </span>
            </div>
        </div>

        <hr/>
    </div>
</div>
    <table class="layui-table" lay-skin="line" id="test">
        <colgroup>
            <col width="155">
            <col width="155">
            <col width="155">
            <col width="155">
            <col width="155">
            <col width="155">
            <col width="155">
            <col width="155">
        </colgroup>
        <thead>
        <tr>
            <th>信息编号</th>
            <th>招聘职位</th>
            <th>招聘人数</th>
            <th>招聘部门</th>
            <th>开始日期</th>
            <th>截止日期</th>
            <th>工作地点</th>
            <th><center>操作</center></th>
        </tr>
        </thead>
        <tbody id="table_tbody">
        </tbody>
    </table>
    <script src="../js/jquery.js"></script>
    <script type="text/javascript" src="../frame/layui/layui.js"></script>
    <script src="../frame/layui/lay/modules/layer.js"></script>
    <script src="../js/dateToString.js"></script>

    <script>
        $(function () {
            // 加载数据
            loadDate("",1);

            // 上一页
            $(".prev").live("click",function () {
                var keyword = $("#sousuo").val();
                // 计算上一页
                var pageNo = $(".current_num").text();
                pageNo = parseInt(pageNo);
                if (pageNo == undefined || pageNo <= 1){
                    pageNo = 1;
                }else{
                    pageNo = pageNo - 1;
                }
                // 加载数据
                loadDate(keyword,pageNo);
            });
            // 下一页
            $(".next").live("click",function () {
                var keyword = $("#sousuo").val();
                var pageNo = $(".current_num").text();
                pageNo = parseInt(pageNo);
                var totalNo = $(".total_num").text();
                totalNo = parseInt(totalNo);
                if (pageNo == undefined){
                    pageNo = 1;
                } else if(pageNo >= totalNo){
                    pageNo = totalNo;
                }else{
                    pageNo = pageNo + 1;
                }
                // 加载数据
                loadDate(keyword,pageNo);
            });
            // 首页
            $(".first").live("click",function () {
                var keyword = $("#sousuo").val();
                var pageNo = 1;
                // 加载数据
                loadDate(keyword,pageNo);
            });
            // 尾页
            $(".last").live("click",function () {
                var keyword = $("#sousuo").val();
                var pageNo = $(".total_num").text();
                pageNo = parseInt(pageNo);
                if (pageNo == undefined){
                    pageNo = 1;
                }
                // 加载数据
                loadDate(keyword,pageNo);
            });
        });
        // 加载数据
        function loadDate(keyword,pageNo) {
            layer.load(1);

            $("#table_tbody").empty();
            $.ajax({
                url: "/Application/queryAll",
                type: "POST",
                data: {
                    keyword:keyword,// 模糊查询字段
                    pageNum:pageNo// 分页参数
                },
                dataType: "json",
                success: function (data) {
                    // console.log(data);
                    // 获取搜索框并赋值，防止模糊搜索时点击上下页数据恢复
                    $("#sousuo").val(keyword);
                    // 加载动画
                    setTimeout(function(){
                        layer.closeAll('loading');
                    }, 200);
                    // 判断数据是否查询成功
                    if(data.success){
                        var applications = data.data.list;
                        // 判断是否有数据
                        if(applications.length>=0) {
                            for (var i = 0; i < applications.length; i++) {
                                $("#table_tbody").append("" +
                                    "<tr>" +
                                    "<td>" + applications[i].appid + "</td>" +
                                    "<td>" + applications[i].positi.posname + "</td>" +
                                    "<td>" + applications[i].appnum + "</td>" +
                                    "<td>" + applications[i].department.depname + "</td>" +
                                    "<td>" + formatDate(applications[i].apptimebegin) + "</td>" +
                                    "<td>" + formatDate(applications[i].apptimeend) + "</td>" +
                                    "<td>" + applications[i].appland + "</td>" +
                                    "<td><center><a href='/application/updateApplication.jsp?" + applications[i].appid + "'>" +
                                    "<button class='layui-btn layui-btn-sm'><i class='layui-icon'>&#xe642;</i></button>" +
                                    "</a>&nbsp;<a href='javascript:del(" + applications[i].appid + ");'>" +
                                    "<button class='layui-btn layui-btn-sm'><i class='layui-icon'>&#xe640;</i>" +
                                    "</button></a></center></td>" +
                                    "</tr>");
                            }
                            $("#table_tbody").append("<tr><td colspan='8'></td></tr>" +
                                "<tr style='border-bottom: 1px solid #AAAAAA; height: 50px;text-align: center;'>" +
                                "<td colspan='8'><div id='page_div'>" +
                                "第 <span class='current_num'>" + data.data.pageNum + "</span>页" +
                                "共 <span class='total_num'>" + data.data.pages + " </span>页" +
                                "每页 " + data.data.pageSize + " 条信息" +
                                "共 " + data.data.total + " 条信息" +
                                "&nbsp;&nbsp;<a href='javascript:(0);' class='first'>首页</a>" +
                                "&nbsp;&nbsp;<a href='javascript:(0);' class='prev'>上一页</a>" +
                                "&nbsp;&nbsp;<a href='javascript:(0);' class='next'>下一页</a>" +
                                "&nbsp;&nbsp;<a href='javascript:(0);' class='last'>尾页</a>" +
                                "</div></td></tr>");
                        }else{
                            $("#table_tbody").append("<tr><td colspan='8'><font color='red'>暂无数据，请添加！</font></td></tr>");
                        }
                    }else{
                        // 错误提示
                        layer.msg(data.message);
                    }
                }
            });
        }
        // 删除数据
        function del(appid) {
            // 询问提示框
            layer.confirm("是否确认删除？", {
                btn: ['确认', '取消'] //按钮
            }, function(){
                // 点击确认执行此方法
                $.ajax({
                    url: "/Application/delete",
                    type: "POST",
                    data: {
                        appid:appid
                    },
                    dataType: "json",
                    success: function (data) {
                        if(data.success){
                            layer.msg(data.message);
                        }else{
                            layer.msg(data.message);
                        }
                    }
                });
                window.location.reload();
            }, function(){
                // 此为取消时执行的方法
            });
        }
        // 模糊查询
        function queryByKey() {
            // 获取搜索框的值
            var keyword = $("#sousuo").val();
            loadDate(keyword,1);
        }
    </script>

</body>
</html>